package com.taku.adprofadapter;

import android.util.Log;

import com.adprof.sdk.api.IBiddingNotify;
import com.adprof.sdk.api.InterstitialAd;
import com.adprof.sdk.api.NativeAd;
import com.adprof.sdk.api.RewardAd;
import com.adprof.sdk.api.SplashAd;
import com.anythink.core.api.ATAdConst;
import com.anythink.core.api.ATBiddingNotice;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class AdprofBiddingNotify implements ATBiddingNotice {

    private static final String TAG = AdprofInitNetwork.TAG;

    IBiddingNotify ibidding;

    protected AdprofBiddingNotify(IBiddingNotify adObject) {
        this.ibidding = adObject;
    }

    // costPrice：竞胜价格
    // secondPrice: 第一位 竞败 的价格, 即 竞胜方后一位的价格（二价）   单位分
    @Override
    public void notifyBidWin(double costPrice, double secondPrice, Map<String, Object> extra) {

        try {
            // notifyBidWin   adType = inter    costPrice = 1999.9999999999998   secondPrice = 1999.9999999999998  extra = {adn_id=5, adn_type=101}
            Log.d(TAG, "\n\n notifyBidWin   adType = " + getAdType() + "    costPrice = " + costPrice + "   secondPrice = " + secondPrice + "  extra = " + extra);

            Map<String, Object> map = new HashMap<>();

            if (extra != null && !extra.isEmpty()) {
                map.putAll(extra);
            }

            map.put(IBiddingNotify.WIN_FIRST_PRICE, costPrice);

            // tobid 是 二价为0
            if (secondPrice != costPrice) {
                map.put(IBiddingNotify.WIN_HIGHEST_LOSS_PRICE, (int) Math.round(secondPrice));
            }

            map.put(IBiddingNotify.MEDIATION, "taku");

            if (ibidding != null) {
                ibidding.sendWinNotify(map);
            }
        } catch (Throwable e) {
            Log.d(TAG, "notifyBidWin: " + Log.getStackTraceString(e));
        }
    }

    // lossCode：竞败码  失败原因，参考 ATAdConst.BIDDING_TYPE 类
    // extra参数：可通过Key: ATBiddingNotice.ADN_ID，从extra中获取竞胜方渠道，竞胜方渠道的枚举值，参考ATAdConst.BIDDING_ADN_ID 类
    @Override
    public void notifyBidLoss(String lossCode, double winPrice, Map<String, Object> extra) {

        try {
            Log.d(TAG, "\n\n  notifyBidLoss adType = " + getAdType() + "     lossCode = " + lossCode + "   winPrice = " + winPrice + "  extra = " + extra);

            Map<String, Object> map = new HashMap<>(4);

            if (extra != null && !extra.isEmpty()) {
                map.putAll(extra);
            }

            map.put(IBiddingNotify.LOSS_FIRST_PRICE, winPrice);

            map.put(IBiddingNotify.MEDIATION, "taku");

            mapPutLossReason(lossCode, map);

            if (ibidding != null) {
                ibidding.sendLossNotify(map);
            }

        } catch (Throwable e) {
            Log.d(TAG, "notifyBidLoss: " + Log.getStackTraceString(e));
        }
    }

    // isWinner：是否为竞胜方
    // displayPrice：正在曝光的广告的价格
    @Override
    public void notifyBidDisplay(boolean isWinner, double displayPrice) {

        Log.d(TAG, "\n\n notifyBidDisplay   adType = " + getAdType() + "    isWinner = " + isWinner + "   displayPrice = " + displayPrice);
    }

    @Override
    public ATAdConst.CURRENCY getNoticePriceCurrency() {
        return ATAdConst.CURRENCY.RMB_CENT;
    }

    private String getAdType() {
        if (ibidding instanceof RewardAd) {
            return "reward";
        }

        if (ibidding instanceof InterstitialAd) {
            return "inter";
        }

        if (ibidding instanceof SplashAd) {
            return "splash";
        }

        if (ibidding instanceof NativeAd) {
            return "native";
        }

        return ibidding != null ? ibidding.toString() : "";
    }

    private static void mapPutLossReason(String lossCode, Map<String, Object> map) {
        if (Objects.equals(lossCode, ATAdConst.BIDDING_TYPE.BIDDING_LOSS_WITH_BIDDING_TIMEOUT)) {

            map.put(IBiddingNotify.LOSS_REASON, IBiddingNotify.AdLoseType.Timeout);

        } else if (Objects.equals(lossCode, ATAdConst.BIDDING_TYPE.BIDDING_LOSS_WITH_LOW_PRICE_IN_HB) ||
                Objects.equals(lossCode, ATAdConst.BIDDING_TYPE.BIDDING_LOSS_WITH_LOW_FLOOR) ||
                Objects.equals(lossCode, ATAdConst.BIDDING_TYPE.BIDDING_LOSS_WITH_LOW_PRICE_IN_NORMAL)) {

            map.put(IBiddingNotify.LOSS_REASON, IBiddingNotify.AdLoseType.LowEcpm);

        } else {
            map.put(IBiddingNotify.LOSS_REASON, IBiddingNotify.AdLoseType.Uknown);
        }
    }

}
